//
//  ContactTableCell.m
//  RZIM
//
//  Created by reyzhang on 2021/8/16.
//

#import "ContactTableCell.h"
#import <YMIMKit/YMIMKit.h>
#import <SDWebImage/SDWebImage.h>
#import <Masonry/Masonry.h>

@interface ContactTableCell ()

@end
 

@implementation ContactTableCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self commonInit];
    }
    return self;
}



- (void)commonInit {
    [self.contentView addSubview:self.checkBtn];
    [self.contentView addSubview:self.portraitView];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.lineView];
    

    
    [self.checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.centerY.equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(20, 22));
    }];
    
    [self.portraitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(55);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.left.mas_equalTo(self.portraitView.mas_right).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.portraitView.mas_right);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-1);
        make.height.mas_equalTo(1);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-15);
            
    }];
    
}


- (void)awakeFromNib {
    
    [super awakeFromNib];
//    [self.checkBtn setImage:[UIImage imageNamed:@"Invoice_Uncheck"] forState:UIControlStateNormal];
//    [self.checkBtn setImage:[UIImage imageNamed:@"Invoice_Checked"] forState:UIControlStateSelected];
//    self.checkBtn.hidden = YES;
//    self.checkBtn.userInteractionEnabled = NO;
//
//    self.portraitView.layer.cornerRadius = 20;
//    self.portraitView.layer.masksToBounds = YES;
//    [self.contentView addSubview:self.lineView];
//    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.left.mas_equalTo(self.portraitView.mas_right);
//        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-1);
//        make.height.mas_equalTo(1);
//        make.right.mas_equalTo(self.contentView.mas_right).offset(-15);
//
//    }];
//
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
 
    [super setSelected:selected animated:animated];
    if (!self.showSingleSelect == YES) {
        
        self.checkBtn.selected = selected;
    }
    if (self.groupSelf == YES) {
        
        self.checkBtn.selected = YES;
        
    }
    
}

- (void)setShowSelect:(BOOL)showSelect {
    _showSelect = showSelect;
    self.checkBtn.hidden = !_showSelect;
    [self.portraitView mas_updateConstraints:^(MASConstraintMaker *make) {
        CGFloat offset = showSelect ? 55 : 15;
        make.left.equalTo(self.contentView).offset(offset);
    }];
//    self.portrait_layout_left.constant = showSelect ? 55 : 15;
}


- (void)bindData:(id)data {
    if ([data isKindOfClass:[YMFriendInfo class]]) {
        YMFriendInfo *userInfo = (YMFriendInfo *)data;
        [self.portraitView sd_setImageWithURL:[NSURL URLWithString:userInfo.friendAvatar] placeholderImage:nil options:SDWebImageRetryFailed];
        NSString *userId = [YMIMClient sharedClient].userInfo.userId;
        if ([userId isEqualToString:userInfo.friendId]) {
            
            self.nameLabel.text = [NSString stringWithFormat:@"%@(自己)",userInfo.friendName];
            self.nameLabel.textColor = [UIColor grayColor];
            
        }else{
            
            self.nameLabel.text = userInfo.friendName;
           
        }
        
    }
}


#pragma mark - Lazy
- (UIButton *)checkBtn {
    if (!_checkBtn) {
        _checkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *image = [UIImage imageNamed:@"Invoice_Uncheck"];
        UIImage *image_sel = [UIImage imageNamed:@"Invoice_Checked"];
        [_checkBtn setImage:image forState:UIControlStateNormal];
        [_checkBtn setImage:image_sel forState:UIControlStateSelected];
    }
    return _checkBtn;
}

- (UIImageView *)portraitView {
    if (!_portraitView) {
        _portraitView = [[UIImageView alloc] init];
        _portraitView.layer.cornerRadius = 20;
        _portraitView.layer.masksToBounds = YES;
    }
    return _portraitView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
        _nameLabel.font = [UIFont systemFontOfSize:15];
    }
    return _nameLabel;
}


-(UIView *)lineView{
    
    if (_lineView == nil) {
        
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:0.1];
    }
    
    return _lineView;
}
@end
